﻿using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace FileOperators
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnOpenWechatExcelFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "C:\\Users\\Admin\\Documents\\WeChat Files\\wxid_mcns0m5jghvf22\\FileStorage\\File\\" + DateTime.Now.ToString("yyyy-MM");
                openFileDialog.Filter = "Excel files | *.xls;*.xlsx;*.csv | All files | *.*";
                openFileDialog.FilterIndex = 0;
                openFileDialog.Title = "选择 Excel 文件";
                openFileDialog.RestoreDirectory = true;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    textBox1.Text = openFileDialog.FileName;
                    OpenExcelFile(filePath);
                }
            }
        }
        private void OpenExcelFile(string filePath)
        {
            try
            {
                Excel.Application excelApp = new Excel.Application();
                Workbook wb = excelApp.Workbooks.Open(filePath);
                excelApp.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开 Excel 文件时出错：" + ex.Message);
            }
        }

        private void btnChangedExcelDate_Click(object sender, EventArgs e)
        {
            using(OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "C:\\Users\\Admin\\Desktop\\AIM組員加班申請單\\SAMPLE\\";
                if(openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    openFileDialog.Title = "更改 Sample.xls 的日期";
                    openFileDialog.RestoreDirectory = true;
                    string filePath = openFileDialog.FileName;
                    ReplaceDateForDay(filePath);
                }
            }
        }
        private void ReplaceDateForDay(string filePath)
        {
            try
            {
                Excel.Application myApp = new Excel.Application();
                Excel.Workbook wb = myApp.Workbooks.Open(filePath);

                Excel.Worksheet ws = wb.Worksheets[1];
                ws.Cells[5, 2] = DateTime.Now.AddDays(-1).ToString("MM/dd");
                wb.Save();
                wb.Close();
                Marshal.ReleaseComObject(wb);
                myApp.Quit();
                MessageBox.Show("修改成功！");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }

}

        private void btnCreateFile_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime dateTime = DateTime.Now;
                string directoryPath = "C:\\Users\\Admin\\Desktop\\AIM組員加班申請單" + "\\" + dateTime.ToString("yyyy-MM-dd_HHmmss_") + "_加班單" + "\\";
                string content = "早上好\r\n" + dateTime.ToString("yyyy-MM-dd ") + "申報昨日加班單\r\n熊濤:無有加班";
                string copyFile = "C:\\Users\\Admin\\Desktop\\AIM組員加班申請單\\SAMPLE" + "\\" + "_Sample.xls";
                Directory.CreateDirectory(directoryPath);
                File.WriteAllText(directoryPath + dateTime.ToString("yyyyMMdd") + "_昨日加班單說明.txt", content);
                File.Copy(copyFile, directoryPath + "OA研發_加班申請單-智二課__" + dateTime.ToString("yyyyMMdd_HHmmss") + ".xls");
                MessageBox.Show("Success！");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("是否退出？", "操作提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    this.Dispose();
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}
